<?php
Class Talons extends CI_Model
{
	
	public function __construct() {
		parent::__construct();
	}
	
	public function abortAllTalon(){
		$talons = $this->getAllTalonCardByGameId();
		$cardsUsed = $this->getTalonCardRevealedByGameId(true, false);
		$data = array("active","false");
		foreach($talons as $talon){
			$this->updateTalonCardByGameId($talon->id, $data);
		}
	}
	
	public function revealedCard($talonId){
		$data = array(
			"revealed"=>true,
			"replenishee_id"=>0
		);
		$this->updateTalonCardByGameId($talonId, $data);
	}
	
	private function updateTalonCardByGameId($talonId,$data){
		$this->db->where('id',$talonId);
		$this->db->update('talon',$data);
	}
	
	public function removeCardsFromTalon($turnCards){
		foreach($turnCards as $card){
			$this->updateTalonCardByGameId($card->talon_id,array("active"=>"false"));
		}
	}
	
	public function replenishPlayer($playerId){
		$newTaloncardsDrawn = $this->getTalonCardByNotEqualToPlayerIdLimitOf();
		$this->updateReplenisheeId($newTaloncardsDrawn, $playerId);
		return $this->getTalonCardByGameIdAndPlayerId($playerId);
	}
	
	private function getTopOfTalonCards(){
		$this->db->where('replenishee_id',0);
		$this->db->where('masterSuit',false);
		return $this->db->get('talon')->first_row();
	}
	
	private function getCurrentMasterSuit(){
		$this->db->where('masterSuit',true);
		return $this->db->get('talon')->first_row();
	}
	
	public function replenishPlayerByOne($playerId){//
		$card = $this->getTopOfTalonCards();
		if(empty($card)){
			$card = $this->getCurrentMasterSuit();
			// if not 0 then this is already occuppied
			if($card->replenishee_id > 0){
				return;
			}
		}
		
		
		
		$updateData = array(
			"replenishee_id" => $playerId
		);
		$this->db->where('id',$card->id);
	  	$this->db->update('talon', $updateData);
	  	return $card;//$this->getLastTalonCardByGameIdAndPlayerId($playerId);
	}
	
	public function getTalonCardByNotEqualToPlayerIdLimitOf(){
		$this->db->where('replenishee_id',0);							// means this talon card was never been drawn or replenished to a player
		return $this->db->get('talon')->result();
	}
	public function getNewSetOfShuffledCards(){
		$this->createTalonCards();
		return $this->getAllTalonCardByGameId();
	}
	public function getAllTalonCardByGameId(){
		$this->db->where('game_id',$this->session->userdata('gameId'));
		return $this->db->get('talon')->result();
	}
	public function getTalonCardRevealedByGameId($revealed,$limit){
		if($limit!=false){
			$this->db->limit($limit);
		}
		$this->db->where('revealed',$revealed);
		$this->db->where('game_id',$this->session->userdata('gameId'));
		return $this->db->get('talon')->result();
	}
	
	public function getLastTalonCardByGameIdAndPlayerId($playerId){
		$this->db->where('replenishee_id',$playerId);
		$query = $this->db->get('talon');
		return $query->last_row();
	}
	
	public function getTalonCardByGameIdAndPlayerId($playerId){		
		$this->db->where('replenishee_id',$playerId);
		//$this->db->where('active',false);
		$this->db->where('revealed',false);
		$query = $this->db->get('talon');
		return $query->result();
	}
	
	public function getTalonCardByGameIdAndPlayerIdNotRevealed($playerId){
		$where = array(
					"replenishee_id"=>$playerId,
					"revealed"=>"false"
				);		
		$this->db->where($where);
		$query = $this->db->get('talon');
		return $query->result();
	}

	public function getTalonCardsByGameId(){							//returns the talons cards created from a game id
		$this->db->order_by('rank', 'RANDOM');
		$this->db->where('game_id',$this->session->userdata('gameId'));
		$query = $this->db->get('talon');
		return $query->result();
	}	
	
	public function createNewSetPlayerCardOnhand($playerId){
		$this->db->limit(6);
		$this->db->where('game_id',$this->session->userdata('gameId'));
		$query = $this->db->get('talon');
		$talonsCards = $query->result();
		$this->updateReplenishee_id($talonsCards, $playerId);
		return $this->getTalonCardByGameIdAndPlayerId($playerId);		// return new set of cards for player
	}
	
	public function endOfAttact($talonCardIds){							// expecting list of talon id
		$CI =& get_instance();
        $CI->load->model('turns');
		foreach ($talonCardIds as $talon) { 							// update stack tracer/table for talon table and  turn table
			$this->removeTalonCardFromGame($talon->id);
			$CI->turns->removeTurnCardFromGame($talon->id);		
		}
		return $this->getActiveTalonCardsByGameId();					// update ui by sending array object then handles by json jquery
	}
	
	private function removeTalonCardFromGame($talonId){
																		// else means that talon card is thrown away from the game
		$updateData = array(
					  	"active" => "false"
					  );
		$this->db->where('id',$talonId);
		$this->db->update('talon', $updateData); 
	}
	
	private function getTopOfTalonCardsForNewMasterSuit(){
		$this->db->where('replenishee_id','0');
		$this->db->where('masterSuit','0');	
		return $this->db->get('talon')->row();
	}
	
	public function getMasterSuitByGameId(){
		$this->db->where('game_id',$this->session->userdata('gameId'));
		$this->db->where('masterSuit',true);
		return $this->db->get('talon')->result();
	}
	
	public function createNewMasterSuit(){
		$data = array('masterSuit'=>true);
		$topCard = $this->getTopOfTalonCardsForNewMasterSuit();
		$this->db->where('id',$topCard->id);
		$this->db->where('game_id',$this->session->userdata('gameId'));
		$this->db->update('talon',$data);
		return $this->getMasterSuitByGameId();
	}
	
//	public function createNewMasterSuit($talonId){
//		$data = array('masterSuit'=>'true');
//		$this->db->where('id',$talonId);
//		$this->db->where('game_id',$this->session->userdata('gameId'));
//		$this->db->update('talon',$data);
//	}
	
	private function updateReplenishee_id($cards, 						//expected to be array of obects
										 $playerId){ 	 
		foreach ($cards as $card) {
				$talonId = property_exists($cards,'talon_id') ? $cards->talon_id : $cards->id;
				$updateData = array(
							  	"replenishee_id" => $playerId,
							  	"revealed" => "true"
							  );
				$this->db->where('id',$talonId);
				$this->db->update('talon', $updateData); 
		}
		return $this->getTalonCardByGameIdAndPlayerId($playerId);
	}
	
	private function getTalonCardById($talonId){
		if($talonId==null){
			return false;
		}
		$this->db->where('id',$talonId);
		return $this->db->get('talon')->first_row();
	}
	
	public function updateReplenishee_idToPlayer($turnCards, 						//expected to be array of obects
										 $playerId){ 	 
		$talonCards_ = array();
		foreach ($turnCards as $card) {
				$talonId = $card->talon_id;
				$updateData = array(
							  	"replenishee_id" => $playerId,
							  	"revealed" => false
							  );
				$this->db->where('id',$talonId);
				$this->db->update('talon', $updateData); 
				array_push($talonCards_, $this->getTalonCardById($talonId));
		}
		$this->db->empty_table('turns'); 
		return $talonCards_;//$this->getTalonCardByGameIdAndPlayerId($playerId);
	}
	
	
	public function get_TalonCardsByGameId(){
		$this->db->where('game_id',$this->session->userdata('gameId'));
		return $this->db->get('talon')->result();
	}
	
	private function createTalonCards(){							//create a shuffled cards containing 36 cards using a game id
		$CI =& get_instance();
        $CI->load->model('card');
		$shuffledCards = $CI->card->getNewSetOfShuffledCards();
		foreach ($shuffledCards as $card) {
			$cardId = $card->id;
			$this->insertTalonCard($cardId);
		}
	}
	
	private function checkGameId($gameId){
		$CI =& get_instance();
        $CI->load->model('games');
		return $CI->games->checkGameId($this->session->userdata('gameId'));
	}
	
	private function insertTalonCard($cardId){
		$data = array(
		   'game_id' => $this->session->userdata('gameId'),
		   'card_id' => $cardId,
		   'active'=>true
		);
		$this->db->insert('talon',$data);
	}	
}
?>